//
//  ST_TodayFateCardsController.swift
//  btvideo
//
//  Created by mac on 2021/1/11.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import NIMSDK

class ST_TodayFateCardsController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .white
        
        ST_BTGlobalDataCenter.shared.todayFateEvent(str: "今日缘分页面曝光")
        
        view.addSubview(top_image_view)
        top_image_view.snp.makeConstraints { (make) in
            make.top.left.equalToSuperview()
            make.right.equalToSuperview().offset(TIANCompatibleValue(value: -44))
            make.height.equalTo(top_image_view.snp.width).dividedBy(1.68)
        }
        
        view.addSubview(close_btn)
        close_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.right.equalToSuperview().offset(-3)
            make.top.equalToSuperview().offset(statusBarHeight)
        }
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(28+statusBarHeight)
            make.height.equalTo(20)
        }
        
        
        
        view.addSubview(countLabel)
        countLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleLabel.snp.right).offset(10)
            make.centerY.equalTo(titleLabel)
            make.height.equalTo(28)
            make.width.equalTo(71)
        }
        
        view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(titleLabel.snp.bottom).offset(19)
            make.height.equalTo(29)
        }
        
        view.addSubview(unlike_btn)
        view.addSubview(like_btn)
        
        let offSet = 10
        
        unlike_btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(80)
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom-15)
            make.right.equalTo(view.snp.centerX).offset(-offSet)
        }
        
        like_btn.snp.makeConstraints { (make) in
            make.bottom.width.height.equalTo(unlike_btn)
            make.left.equalTo(view.snp.centerX).offset(offSet)
        }
        
        
        view.addSubview(free_image_view)
        free_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 45, height: 23))
            make.top.equalTo(like_btn).offset(-8)
            make.right.equalTo(like_btn).offset(4)
        }
        
        
        view.addSubview(card_stack_view)
        card_stack_view.snp.makeConstraints { (make) in
            
            make.height.equalTo(cardHeight)
            make.left.equalToSuperview().offset(4)
            make.right.equalToSuperview().offset(-3)
            make.top.equalToSuperview().offset(142-20+statusBarHeight)
        }
       
        update_count_label()
    }
    

    fileprivate func update_count_label(){
        
        let s = "还有\(totalCount)人"
        
        countLabel.text = s
        
        let w = s.width(withConstrainedHeight: 100, font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)) + 20
        
        countLabel.snp.updateConstraints { (make) in
            make.width.equalTo(w)
        }
        
    }
    
    // MARK: - Properties
    fileprivate lazy var card_stack_view:SwipeCardStack = {
        let control = SwipeCardStack()
        control.delegate = self
        control.dataSource = self
        control.cardStackInsets = UIEdgeInsets.zero
        return control
    }()
    
    
    fileprivate lazy var cardHeight:CGFloat = ScreenHeight-142+20-statusBarHeight-105-BTSafeAreaBottom
    
    fileprivate lazy var personal_sub_label:UILabel = {
        let control = UILabel()
        return control
    }()
    
    
    fileprivate lazy var unlike_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("个人中心_icon_不喜欢")!)
        control.add_touch_up(insideTarget: self, action: #selector(unlike_handle))
        
        return control
    }()
    
    fileprivate lazy var like_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("TodayFateCardsTopImageFreeLike")!)
        control.add_touch_up(insideTarget: self, action: #selector(like_handle))
        return control
    }()
    
    fileprivate lazy var close_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.setImageForAllStates(ST_ImageUtils.imageName("TodayFateCardsFreeCloseImage")!)
        control.add_touch_up(insideTarget: self, action: #selector(click_close_handle))
        return control
    }()
    
    
    fileprivate lazy var free_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("TodayFateCardsFreeLabelImage"))
              control.contentMode = .scaleAspectFill
              return control
          }()
    
    
    fileprivate lazy var top_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("TodayFateCardsTopImage"))
              control.contentMode = .scaleAspectFill
              return control
          }()
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "今日缘分"
        control.textColor = UIColor.white
        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var countLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = .white
        control.textAlignment = .center
        control.cornerRadius = 14
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.backgroundColor = UIColor.init(white: 0, alpha: 0.1)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        if let user = ST_BTUserInfoManager.shared.current_account_info,user.userinfo?.sex == .female{
            control.text = "对你感兴趣的在线精选男生"
        }else{
            control.text = "对你感兴趣的在线精选女生"
        }
        control.textColor = UIColor.white
        control.font = UIFont.systemFont(ofSize: 21, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var totalCount = cards_array.count
    
    var cards_array = [ST_PairsLoveItemViewModel]()
    
    fileprivate lazy var undoSet = Set<String>()


}

extension ST_TodayFateCardsController:SwipeCardStackDelegate, SwipeCardStackDataSource{
    
    
    func cardStack(_ cardStack: SwipeCardStack, didSwipeCardAt index: Int, with direction: SwipeDirection) {
        
        
        if direction == .left {
            ST_BTGlobalDataCenter.shared.todayFateEvent(str: "左滑")
        }else if direction == .right{
            ST_BTGlobalDataCenter.shared.todayFateEvent(str: "右滑")
        }
        
        

        
        self.swipeIndex(index: index, direction: direction)
        
        
    }
    
    fileprivate func swipeIndex(index:Int,direction: SwipeDirection){
        
       
        guard let m =  self.cards_array[safe:index] else {
            
            self.dismiss_handle()
            
            return
        }
        
        
        self.totalCount -= 1
        
        update_count_label()
        
        if direction == .right {
            
//            改为搭讪
            
            if BTVerticalAlertViewController.toastBindPhoneRequireAlert(fromController: self) {
                return
            }
            
            ST_TianAccostManager.shared.accost(idSet: [m.accid], viewController: self,success:  { (json) in
                
            })
            
            
        }
        
        if card_stack_view.topCardIndex == nil{
            
            self.dismiss_handle()
        }
        
    }
    
    
    func cardStack(_ cardStack: SwipeCardStack, cardForIndexAt index: Int) -> SwipeCard {
        
        let card = SwipeCard()
        
        card.swipeDirections = [.left, .right]
        for direction in card.swipeDirections {
          card.setOverlay(ST_TinderCardOverlay(direction: direction), forDirection: direction)
        }
        
//        card.delegate = self
        
        
        let itemView = ST_AIRecommendCardView.init(frame: CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: ScreenWidth - 8, height: cardHeight)))
        
        itemView.from_receive_love = false
        
        itemView.fromTodayCards = true
        
        let model = cards_array[index]
        
        itemView.bind_view_data_set(model: model)
        
        card.content = itemView
        
        return card
        
    }
    
    func numberOfCards(in cardStack: SwipeCardStack) -> Int {
        return cards_array.count
    }
    

}

extension ST_TodayFateCardsController{
    
    @objc fileprivate func unlike_handle(){
        
        
        card_stack_view.swipe(SwipeDirection.left, animated: true)
        
        ST_BTGlobalDataCenter.shared.todayFateEvent(str: "不喜欢（按钮）")
        
    }
    @objc fileprivate func like_handle(){
        
        card_stack_view.swipe(SwipeDirection.right, animated: true)
        
        
        ST_BTGlobalDataCenter.shared.todayFateEvent(str: "SayHi")
    }
    
    @objc fileprivate func click_close_handle(){
        
        
        ST_BTGlobalDataCenter.shared.todayFateEvent(str: "退出")
        
        dismiss_handle()
        
    }
    
    @objc fileprivate func dismiss_handle(){
        
        ST_FateAlertManager.shared.setCurrentAccountFateState(isFinish: true)
        
        self.goback_previous_controller(animated: true) {
            ST_FateAlertManager.shared.dispatch()
        }
        
    }
    
}
